package twentytwenty;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-11 12:56
 * @Modified By:
 */
public class AddDigits_258 {
    public static void main(String[] args) {
        int num = 381;
        System.out.println(addDigits(num));
    }

    public static int addDigits(int num) {
        if (num < 10){
            return num;
        }
        String numL = num + "";
        int numLength = numL.length();
        int [] arr = new int[numLength];
        int fuck = 1;
        for (int i = 1; i < numLength; i++) {
            fuck *= 10;
        }
        //获取每一位存放到arr数组中
        for (int i = 0; i < numLength; i++) {
            arr[i] = num / fuck;
            num %= fuck;
            fuck /= 10;
        }
        int sum = 0;
        // 计算各个位数相加
        for (int i = 0; i < numLength; i++) {
            sum += arr[i];
        }
        return addDigits(sum);
    }
}
